package eworldpacs.ui;

import com.eworld.pacsviewer.R;

import leadtools.dicom.DicomDataSet;
import leadtools.dicom.DicomElement;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class ElementViewActivity extends Activity implements ILeadTreeNodeClickedDelegate {
   
   DicomDataSet _loadedDataSet;
   GridView _currentElementOptionsGridView;
   TabHost _tabHost;
   TabSpec _listViewTabSpec;
   TabSpec _moduleViewTabSpec;
   TextView _progressText;
   LeadTreeView _listTreeView;
   LeadTreeView _moduleTreeView;
   boolean _performingGridViewUpdate = false;
   boolean _activityStarted = false;

   @Override
   protected void onResume() {
      super.onResume();
      if (!_activityStarted){
         _activityStarted = true;
         buildElementViews();
      }
      _tabHost.invalidate();
   }
   
   
   @Override
   public void onLeadTreeNodeClicked(LeadTreeNode node) {
      updateValueGridView(node);
   }
   
   @Override
   public void onPause(){
      super.onPause();
      _activityStarted = false;
   }
   
   
   private void buildElementViews(){
       _loadedDataSet = ((DicomDemoApplication)getApplicationContext()).getLoadedDataSet();		 	     
        setContentView(R.layout.element_view);
        
        String[] gridViewItems = new String[] { 
                                           "Tag"   , " ",
                                           "Name"  , " ",
                                           "VR"    , " ",
                                           "Length", " ",
                                           "Value" , " "  }; 

      _currentElementOptionsGridView = (GridView)findViewById(R.id.elementViewGridView);		 
      _progressText = (TextView)findViewById(R.id.progressText);
      
      ElementViewAdapter adapter = new ElementViewAdapter(this, android.R.layout.simple_list_item_1, gridViewItems);
      
      _currentElementOptionsGridView.setAdapter(adapter);
      
      _currentElementOptionsGridView.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
               Toast.makeText(getApplicationContext(),
               ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
            }
         });
      
      _tabHost = (TabHost)findViewById(android.R.id.tabhost);
      _tabHost.setup();
      _listViewTabSpec = _tabHost.newTabSpec("listview");
      
      
      final ProgressBar mainProgressBar = (ProgressBar)this.findViewById(R.id.mainProgressBar);
      mainProgressBar.setIndeterminate(true);
      mainProgressBar.setVisibility(View.VISIBLE);
      
      
      _listTreeView = new LeadTreeView(this, this);
      FillLeadTreeArgs listModeArgs = new FillLeadTreeArgs(this,_loadedDataSet, _listTreeView, FillLeadTreeArgs.LIST_MODE);
      CreateTreeAsyncTask listModeBuildTask = new CreateTreeAsyncTask();
      listModeBuildTask.setCompletedListener(new OnLeadTreeBuildCompletedListener(){
         @Override
         public void onBuildCompleted(LeadTreeView treeView, Object caller){
            if (treeView != null){
                  TabViewFactory listTabViewFactory = new TabViewFactory(treeView.getView());
                  _listViewTabSpec.setIndicator("List View").setContent(listTabViewFactory);
                  _tabHost.addTab(_listViewTabSpec);
               }
            }
         });
       listModeBuildTask.execute(listModeArgs);
       
       _moduleViewTabSpec = _tabHost.newTabSpec("moduleview");
       _moduleTreeView = new LeadTreeView(this, this);
       FillLeadTreeArgs moduleModeArgs = new FillLeadTreeArgs(this,_loadedDataSet, _moduleTreeView, FillLeadTreeArgs.MODULE_MODE);
       CreateTreeAsyncTask moduleModeBuildTask = new CreateTreeAsyncTask();
       moduleModeBuildTask.setCompletedListener(new OnLeadTreeBuildCompletedListener(){
          @Override
          public void onBuildCompleted(LeadTreeView treeView, Object caller){
             TabViewFactory moduleTabViewFactory = new TabViewFactory(treeView.getView());
             _moduleViewTabSpec.setIndicator("Module View").setContent(moduleTabViewFactory);
             _tabHost.addTab(_moduleViewTabSpec);
             
             
             //do this on the last tree build completion.
             _tabHost.setOnTabChangedListener(new OnTabChangeListener(){
               @Override
               public void onTabChanged(String tabId) {
                  if (tabId == "listview"){
                     //LeadTreeNode selectedNode = listTreeView.getSelectedNode();
                     //updateValueGridView(selectedNode);
                  }
                  else if (tabId == "moduleview"){
                     LeadTreeNode selectedNode = _moduleTreeView.getSelectedNode();
                     updateValueGridView(selectedNode);
                  }
               }
             });
             mainProgressBar.setVisibility(View.GONE);
             _progressText.setVisibility(View.GONE);
             _tabHost.setVisibility(View.VISIBLE);
             _currentElementOptionsGridView.setVisibility(View.VISIBLE);
          }
       });
       moduleModeBuildTask.execute(moduleModeArgs);
   }
   
   private void updateValueGridView(LeadTreeNode node){
      if (!_performingGridViewUpdate){
         _performingGridViewUpdate = true;
         if (_currentElementOptionsGridView != null){
            ElementViewAdapter adapter = (ElementViewAdapter)_currentElementOptionsGridView.getAdapter();
            if (node != null){
               Object tag = node.getTag();
               if (tag instanceof DicomElement){
                  DicomElement element = (DicomElement)tag;
                  ElementInfo info = ElementInfo.create(_loadedDataSet, element, 64);
                  adapter.setValueAtIndex(ElementViewAdapter.TAG_INDEX, info.getTag());
                  adapter.setValueAtIndex(ElementViewAdapter.NAME_INDEX, info.getName());
                  adapter.setValueAtIndex(ElementViewAdapter.VR_INDEX, info.getVR());
                  adapter.setValueAtIndex(ElementViewAdapter.LENGTH_INDEX, info.getLength());
                  adapter.setValueAtIndex(ElementViewAdapter.VALUE_INDEX, info.getValue());
               } else {
                  adapter.setEmptyElementValues();
               }
            } else {
               adapter.setEmptyElementValues();
            }
         }
         _performingGridViewUpdate = false;
      }
   }
}
